<template>
  <div
    id="app"
    style="position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;"
  >
    <transition :name="transitionName">
      <router-view
        class="Router"
        style="margin: 0 auto;"
        @socketConect="socketConect"
        @socketDisConect="socketDisConect"
      ></router-view>
    </transition>
  </div>
</template>

<script>
import {
  SET_PV,
  USER_NAME,
  ACCESS_TOKEN,
  PUSH_HOST,
  PROXY
} from "@/store/mutation-types";
import {
  API_HOST,
  DOM_TITLE,
  WEB_QQ,
  WEB_TEL,
  COPY_RIGHT,
  CHART_URL
} from "@/store/mutation-types";
import moment from "moment";
import "moment/locale/zh-cn";
import { setPvApi, openconfApi } from "@/api";
export default {
  name: "App",
  mounted() {
    var win_w = document.body.offsetWidth;
    // 创建script标签，引入外部文件

    //定义变量
    var fontSize;
    if (win_w > 460) {
      fontSize = 44;

      document.getElementsByTagName("html")[0].style.fontSize = fontSize + "px";
    }

    let pv = this.$ls.get(SET_PV);
    this.username = this.$ls.get(USER_NAME) || "";
    this.setConf();
    this.socketConect();
    if (this.$ls.get(API_HOST)) {
    }
    if (
      !pv ||
      moment().format("YYYY-MM-DD") != moment(pv).format("YYYY-MM-DD")
    ) {
      setPvApi().then(res => {
        this.$ls.set(SET_PV, moment().format("YYYY-MM-DD"));
      });
    }
    // if (this.$ls.get(ACCESS_TOKEN)) {
    //   this.$store.dispatch("user/GetInfo");
    //   //
    // }
  },
  beforeDestroy() {
    this.socket.close();
    clearTimeout(this.heartbeatTimer);
  },
  methods: {
    setConf() {
      this.$store.dispatch("GET_CONF").then(res => {
        if (res.code == 200) {
          let header = document.getElementsByTagName("head")[0];
          let metaKeyword = document.createElement("meta");
          let metaDesc = document.createElement("meta");
          let link = document.createElement("link");

          metaKeyword.name = "keywords";
          metaKeyword.content = res.data.keywords;
          link.type = "image/x-icon";
          link.rel = "shortcut icon";
          link.href = res.data.apihost + res.data.webico;

          metaDesc.name = "description";
          metaDesc.content = res.data.description;
          header.appendChild(metaKeyword);
          header.appendChild(metaDesc);
          header.appendChild(link);

          let app_js = document.createElement("script");
          app_js.type = "text/javascript";
          app_js.src = res.data.apihost + res.data.app_js;
          document.getElementsByTagName("head")[0].appendChild(app_js);
          let chat_js = document.createElement("script");
          chat_js.type = "text/javascript";
          chat_js.src = res.data.apihost + res.data.chat_js;
          document.getElementsByTagName("head")[0].appendChild(chat_js);
        }
      });
    },
    heartbeat() {
      this.heartbeatTimer = setTimeout(() => {
        this.socket.send("ping");
      }, 10000);
    },
    socketConect() {
      this.username = this.$ls.get(USER_NAME) || "";

      if (this.username) {
        const wsuri = "wss://baidu.com/ws?token=" + this.username;
        this.socket = new WebSocket(wsuri);
        let _this = this;
        this.socket.onopen = function(e) {
          console.log("连接服务器成功");
          _this.heartbeat();
        };
        this.socket.onmessage = function(data) {
          _this.heartbeat();
        };
        this.socket.onclose = function(e) {
          console.log("关闭连接");
        };
      }
    },
    socketDisConect() {
      if (this.socket) {
        this.socket.close();
      }
    }
  },
  data() {
    return {
      username: "",
      transitionName: ""
    };
  },
  watch: {
    $route() {
      if (this.$router.isleft) {
        this.transitionName = "slideleft";
      }
      if (this.$router.isright) {
        this.transitionName = "slideright";
      }
    }
  }
};
</script>

<style lang="less">
@import "~styles/variable.less";
@import "~styles/index.less";
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
  -moz-osx-font-smoothing: grayscale;
  margin: 0 auto;
  color: #2c3e50;
}
.Router {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  width: 100%;
  height: 100%;
  max-width: 600px;
  background-color: @body-bg-color;
  transition: all 0.5s ease;
  -webkit-transition: all 0.5s ease;
  -moz-transition: all 0.5s ease;
}
.slideleft-enter,
.slideright-leave-active {
  opacity: 1;
  transform: translate3d(90% 0, 0);
  -webkit-transform: translate3d(90%, 0, 0);
  -moz-transform: translate3d(90%, 0, 0);
}
.slideleft-leave-active,
.slideright-enter {
  opacity: 1;
  transform: translate3d(-90% 0, 0);
  -webkit-transform: translate3d(-90%, 0, 0);
  -moz-transform: translate3d(-90%, 0, 0);
}
</style>
